package model

import (
	"github.com/jinzhu/gorm"
)

type User struct {
	*Model
	UserName     string `json:"userName" gorm:"Column:userName"`
	UserPassword string `json:"userPassword" gorm:"Column:userPassword"`
}

type tokenInfo struct {
	Token string `json:"token"`
}

type versionInfo struct {
	Version string `json:"version"`
}

type TokenInfoSwagger struct {
	*SwaggerSuccess
	Data *tokenInfo `json:"data"`
}

type VersionInfoSwagger struct {
	*SwaggerSuccess
	Data *versionInfo `json:"data"`
}

func (u User) TableName() string {
	return "t_user"
}

func GetUserByName(db *gorm.DB, userName string) (uint32, error) {
	var user User

	db = db.Where("userName = ? and isDelete = 0", userName)
	err := db.First(&user).Error

	if err != nil && err != gorm.ErrRecordNotFound {
		return 0, err
	}

	return user.Model.ID, nil
}

func GetUser(db *gorm.DB, userName, userPassword string) (uint32, error) {
	var user User

	db = db.Where("userName = ? and userPassword = ? and isDelete = 0", userName, userPassword)
	err := db.First(&user).Error

	if err != nil && err != gorm.ErrRecordNotFound {
		return 0, err
	}

	return user.Model.ID, nil
}
